package org.moore.view;


import org.moore.utils.Data;

import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;

// 欢迎界面自定义面板
public class WelcomePanel extends JPanel {
    // 定义界面布局
	private static CardLayout cardLayout;
    // 定义欢迎界面
    private static WelcomeJLabel welcomeJLabel;
    // 定义登陆界面
    private static LoginJLabel loginJLabel;
    // 定义注册界面
    private static RegisterJLabel registerJLabel;
    // 定义可自定义透明度的图片
    private BufferedImage image;
    // 定义gif读取图片
    private ImageReader gifReader;
    // 定义gif初始帧数
    private int gifFrame = 0;
    // 定义是否播放gif
    private boolean isGifPlaying = false;
    // 定义透明度
    private float alpha = 1f;
    // 定义用于播放gif的定时器
    private Timer gifTimer;

    public static CardLayout getCardLayout() {
        return cardLayout;
    }

    public void setCardLayout(CardLayout cardLayout) {
        WelcomePanel.cardLayout = cardLayout;
    }

    public static WelcomeJLabel getWelcomeJLabel() {
        return welcomeJLabel;
    }

    public void setWelcomeJLabel(WelcomeJLabel welcomeJLabel) {
        WelcomePanel.welcomeJLabel = welcomeJLabel;
    }

    public static LoginJLabel getLoginJLabel() {
        return loginJLabel;
    }

    public static void setLoginJLabel(LoginJLabel loginJLabel) {
        WelcomePanel.loginJLabel = loginJLabel;
    }

    public static RegisterJLabel getRegisterJLabel() {
        return registerJLabel;
    }

    public static void setRegisterJLabel(RegisterJLabel registerJLabel) {
        WelcomePanel.registerJLabel = registerJLabel;
    }

    public WelcomePanel() {
        // 设置布局管理器
        cardLayout = new CardLayout();
        this.setLayout(cardLayout);
        // 获取图片
        try {
            image = ImageIO.read(new File("Image/logo.png"));
            ImageInputStream gifStream = ImageIO.createImageInputStream(new File("Image/456456_3840x2160_ahq-11.gif"));
            Iterator<ImageReader> readers = ImageIO.getImageReaders(gifStream);
            if (readers.hasNext()) {
                gifReader = readers.next();
                gifReader.setInput(gifStream);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 新建计时器修改透明度实现淡入淡出
        Timer timer = new Timer(20, new ActionListener() {
            boolean fadingOut = true;
            public void actionPerformed(ActionEvent e) {
                if (fadingOut) {
                    alpha -= 0.01f;
                    if (alpha <= 0) {
                        alpha = 0;
                        fadingOut = false;
                        isGifPlaying = true;
                        startGifTimer();
                    }
                } else {
                    alpha += 0.01f;
                    if (alpha >= 1) {
                        alpha = 1;
                        fadingOut = true;
                        ((Timer) e.getSource()).stop();
                        loadLabel();
                    }
                }
                repaint();
            }
        });
        timer.start();
    }

    // 新建计时器显示gif的方法
    private void startGifTimer() {
        gifTimer = new Timer(40, e -> {
            gifFrame++;
            try {
                if (gifFrame >= gifReader.getNumImages(true)) {
                    gifFrame = 0;
                    isGifPlaying = false;
                    ((Timer) e.getSource()).stop();
                }
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
            repaint();
        });
        gifTimer.start();
    }

    // 新建面板的方法
    private void loadLabel() {
        // 创建Label对象
        welcomeJLabel = new WelcomeJLabel();
        loginJLabel = new LoginJLabel();
        registerJLabel = new RegisterJLabel();
        // 设置图片
        welcomeJLabel.setIcon(Data.WELCOME_IMAGEICON);
        // 将JLabel设置为JPanel的组件
        this.add(welcomeJLabel, "Welcome");
        this.add(loginJLabel,"Login");
        this.add(registerJLabel, "Register");
        revalidate();
        repaint();
    }

    // 绘制动画
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(Data.BLACK_BACKGROUND,0,0,this.getWidth(),this.getHeight(),null);
        Graphics2D g2d = (Graphics2D) g.create();
        g2d.setComposite(AlphaComposite.SrcOver.derive(alpha));
        int x = (getWidth() - image.getWidth()) / 2;
        int y = (getHeight() - image.getHeight()) / 2;
        if (isGifPlaying) {
            try {
                BufferedImage gifFrameImage = gifReader.read(gifFrame);
                g2d.drawImage(gifFrameImage, x, y, this);
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            g2d.drawImage(image, x, y, this);
        }
        g2d.dispose();
    }
}
